/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.${module};

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.${module}.${classname};
import com.unis.service.${module}.${classname}Service;

/**
 * @author ${user} (email:${email})
 * @since ${createDate}
 */
<#assign instance = "${classname?lower_case}">
@Controller
@RequestMapping("/${instance}")
public class ${classname}Controller {

    private ${classname}Service ${instance}Service;

    /****************************************
     * Insert a instace of ${classname}
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insert${classname}(@RequestBody ${classname} ${instance}) throws Exception {
        ${instance}.setStatmentId("${classname}.insert");
        String status = "failure";
        ${instance}Service.insert(${instance});
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of ${classname}
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody ${classname} ${instance}) throws Exception {
        ${instance}.setStatmentId("${classname}.update");
        String status = "failure";
        ${instance}Service.update(${instance});
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of ${classname}
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody ${classname} ${instance}) throws Exception {
        ${instance}.setStatmentId("${classname}.delete");
        String status = "failure";
        ${instance}Service.batchDelete(${instance});
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of ${classname}
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        ${classname} ${instance} = new ${classname}();
        ${instance}.setStatmentId("${classname}.queryAsList");
        BeanUtils.populate(${instance}, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(${instance}, pageMap);
        List<BaseModel> ${instance}List = ${instance}Service.queryAsList(${instance});
        Map<String, Object> renderMap = GridUIMapper.render(${instance}, ${instance}List);
        return renderMap;
    }

    /****************************************
     * Query a instaces of ${classname}
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody ${classname} ${instance}) throws Exception {
        ${instance}.setStatmentId("${classname}.queryAsObject");
        return ${instance}Service.queryAsObject(${instance});
    }

    @RequestMapping("/page")
    public String forword() {
        return "${module}/${instance}List";
    }

    @Resource
    public void set${classname}Service(${classname}Service ${instance}Service) {
        this.${instance}Service = ${instance}Service;
    }
}
